package com.piece.mall.controller;

import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.log.annotation.OperationType;
import com.piece.core.log.enums.AuditLogType;
import com.piece.core.pay.dto.PayNotifyDataDTO;
import com.piece.core.pay.enums.PayChannelType;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.mall.service.OmsOrderService;
import com.piece.mall.service.PaymentService;
import com.piece.mall.service.PmsSkuInfoService;
import com.piece.mall.service.PmsSpuInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@Api(tags = {"支付回调操作管理"})
@RequestMapping("/public/mall/payment/notify")
public class PaymentNotifyController extends FrameWorkController {

    @Resource
    private PaymentService paymentService;

    @Resource
    private PmsSpuInfoService pmsSpuInfoService;

    @Resource
    private PmsSkuInfoService pmsSkuInfoService;

    @Resource
    private OmsOrderService omsOrderService;

    @RequestMapping("/payoff/alipay")
    @ApiOperation(value = "支付宝支付通知")
    @OperationType(AuditLogType.OTHER)
    public AjaxResponse alipayNotify(HttpServletRequest request, HttpServletResponse response) {
        PayNotifyDataDTO notifyData = PayNotifyDataDTO.builder().request(request).response(response).build();
        notifyData.setBody(request.getQueryString());
        try {
            return paymentService.payNotify("1", PayChannelType.ALIPAY_PC.getCode(), notifyData);
        } catch (Exception e) {
            return AjaxResponse.error(e.getMessage());
        }
    }

    @RequestMapping("/payoff/wxpay")
    @ApiOperation(value = "微信支付通知")
    @OperationType(AuditLogType.OTHER)
    public AjaxResponse wxpayNotify(HttpServletRequest request, HttpServletResponse response) {
        PayNotifyDataDTO notifyData = PayNotifyDataDTO.builder().request(request).response(response).build();
        try {
            return paymentService.payNotify("2", PayChannelType.WX_NATIVE.getCode(), notifyData);
        } catch (Exception e) {
            return AjaxResponse.error(e.getMessage());
        }
    }

    @RequestMapping("/refund/alipay")
    @ApiOperation(value = "支付宝退款通知")
    @OperationType(AuditLogType.OTHER)
    public AjaxResponse alipayRefund(HttpServletRequest request, HttpServletResponse response) {
        PayNotifyDataDTO notifyData = PayNotifyDataDTO.builder().request(request).response(response).build();
        notifyData.setBody(request.getQueryString());
        try {
            return paymentService.refundNotify("1", PayChannelType.ALIPAY_PC.getCode(), notifyData);
        } catch (Exception e) {
            return AjaxResponse.error(e.getMessage());
        }
    }

    @RequestMapping("/refund/wxpay")
    @ApiOperation(value = "微信退款通知")
    @OperationType(AuditLogType.OTHER)
    public AjaxResponse wxpayRefund(HttpServletRequest request, HttpServletResponse response) {
        PayNotifyDataDTO notifyData = PayNotifyDataDTO.builder().request(request).response(response).build();
        try {
            return paymentService.refundNotify("2", PayChannelType.WX_NATIVE.getCode(), notifyData);
        } catch (Exception e) {
            return AjaxResponse.error(e.getMessage());
        }
    }
}
